<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, viewport-fit=cover" />
		<title>支付定金</title>
		<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, user-scalable=no">
		<link rel="stylesheet" type="text/css" href="css/info.css?id=2" />
		<link rel="stylesheet" type="text/css" href="css/every.css?id=2" />
	</head>
	<body>
		<!--  -->
		<div class="wrap">
			<div class="header">
				<span class="shu"></span>
				<b>收货信息</b></div>
			<div class="box disRow_S">
				<div class="tit">联系人</div>
				<input id="name" readonly="readonly" readonly style="margin-left: 0.8rem;" />
			</div>
			<div class="box disRow_S">
				<div class="tit">手机号码</div>
				<input id="tel" readonly="readonly" readonly style="margin-left: 0.5rem;" />
			</div>

			<div class="box disRow_S">
				<div class="tit">所在地区 </div>
				<input type="text" readonly="readonly" readonly class="input" readonly id="myAddrs" placeholder="省 市 区 街办" name="addr" data-key="27-438-37" value="" />
			</div>
			<div class="box disRow_S">
				<div class="tit">详细地址 </div>
				<input type="text" readonly="readonly" readonly id="adds" class="input" placeholder="所在地街道、楼牌号等" />
			</div>
		
			<input type="hidden" id="is" value="" />
			<input type="hidden" id="battery_id" value="" />
			<!--保存地址-->
		</div>
		<div style=" font-size: 0.3rem; color: #D92735; margin-top: 0.5rem; padding-left: 0.3rem; padding-right: 0.1rem;">
			<p>订金说明:</p>
			<p>1、参与换电池活动需支付50元定金，本定金支付后可随时退款；</p>
			<p>2、本定金仅抵扣电池费用，确认电池型号后补差价即可获得全新电池+充电卡券；</p>
			<p>3、支付定金后12小时内工作人员上门为您服务！</p>	
		</div>
		<div class="footer" id="savebox">
			<div class="save">支付订金</div>
		</div>
	</body>
	<script src="js/tui.js"></script>
	<script src="js/jq.js"></script>
	<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
	<script>
		(function($) {
			//获取参数
			$.getUrlParam = function(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null) return decodeURI(r[2]);
				return null;
			}

		})(jQuery);
		// uid为上一个页面传过来的参名
		var uid = $.getUrlParam('locked');
		var u = navigator.userAgent;
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
		$('.save').click(function() {
			var name = $('#name').val();
			var tel = $('#tel').val();
			var myAddrs = $('#myAddrs').val();
			var adds = $('#adds').val();
			if (name == '' || tel == '' || myAddrs == '' || adds == '') {
				if (isWeiXin()) {
					 layer.alert('请将信息填写完整再提交');
				}
				if (isiOS) {
					//ios
					window.webkit.messageHandlers.showAlert.postMessage('请将信息填写完整再提交');
					return
				}
				if (isAndroid) {
					//Android
					window.TiantianCharge_Android.showAlert('请将信息填写完整再提交');
					return
				}
			}
			if (/^1[3456789]\d{9}$/.test(tel) === false) {
				if (isWeiXin()) {
					
					layer.alert('请输入正确的手机号码');
				}
				if (isiOS) {
					//ios
					window.webkit.messageHandlers.showAlert.postMessage('请输入正确的手机号码');
					return
				}
				if (isAndroid) {
					//Android
					window.TiantianCharge_Android.showAlert('请输入正确的手机号码');
					return
				}

			}
			var is = $('#is').val();
			var ids = $('#battery_id').val(); //换购电池订单
			if (is == 1) {
				//不提交
				var p = 5000;
				if (isWeiXin()) {
					// 判断小程序的环境
					if (window.__wxjs_environment === 'miniprogram') {
						wx.miniProgram.reLaunch({
							url: `/pages/pay/pay?fee=${p} + &orderId=${ids}`
						})
					}
				}
				//判断机型
				if (isiOS) {
					//ios 支付
					var pay = p + '!' + ids; //支付参数拼接，请勿修改格式
					window.webkit.messageHandlers.pay.postMessage(pay);
				}
				if (isAndroid) {
					//Android 支付
					window.TiantianCharge_Android.pay(p, ids);
				}
			} else {
				$.ajax({
					type: "POST",
					url: "https://api.cd1a.cn/index.php/activity/swap4/add_battery",
					dataType: "json",
					data: {
						userid_locked: uid,
						name: name,
						phone: tel,
						area: myAddrs,
						address: adds
					},
					success: function(res) {
						if (res.result_code == 200) {
							var battery_id = res.data.battery_id; //换购电池订单
							var pr = 5000;
							$("#is").attr("value",1);
							$("#battery_id").attr("value",battery_id);
							if (isWeiXin()) {
								// 判断小程序的环境
								if (window.__wxjs_environment === 'miniprogram') {
									wx.miniProgram.reLaunch({
										url: `/pages/pay/pay?fee=${pr} + &orderId=${battery_id}`
									})
								}
							}
							//判断机型
							if (isiOS) {
								//ios 支付
								var pay = pr + '!' + battery_id; //支付参数拼接，请勿修改格式
								window.webkit.messageHandlers.pay.postMessage(pay);
							}
							if (isAndroid) {
								//Android 支付
								window.TiantianCharge_Android.pay('5000', battery_id);
							}

						}

					}
				})
			}
		})

		//判断微信
		function isWeiXin() {
			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/MicroMessenger/i) == 'micromessenger') {
				return true; // 是微信端
			} else {
				return false;
			}
		}
		//判断是否有订单
		setTimeout(function() {
			$.ajax({
				type: "POST",
				url: "https://api.cd1a.cn/index.php/activity/swap4/user_order",
				data: {
					userid_locked: uid
				},
				dataType: "json",
				async: true,
				success: function(res) {
					if (res.result_code == 200) {
						var data = res.data;
						if (data == null) {
							return
						}
						if (data.course <4) {
							$("#name").attr("value", data.name);
							$("#tel").attr("value", data.phone);
							$("#myAddrs").attr("value", data.area);
							$("#adds").attr("value", data.address);
							$("#is").attr("value",1);
							$("#battery_id").attr("value", data.battery_id);
						}
					}

				}
			});
		}, 50);
	</script>


</html>
